package com.huangwei.service.impl;

import com.huangwei.dao.CarDao;
import com.huangwei.dao.NavDao;
import com.huangwei.dao.RecommendDao;
import com.huangwei.model.Car;
import com.huangwei.model.Nav;
import com.huangwei.model.Recommend;
import com.huangwei.service.RecommendService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
public class RecommendServiceImpl implements RecommendService {
    @Resource
    RecommendDao recommendDao;
    @Resource
    CarDao carDao;
    @Resource
    NavDao navDao;
    @Override
    public List<Recommend> getRecommendsByUser(int userId) {
        try {
            return recommendDao.queryByUser(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public List getRecommendCars(int userId) {
        List<Recommend> recommendList = getRecommendsByUser(userId);
        List<Car> carList = new LinkedList<>();
        recommendList.stream()
                .forEach(recommend -> {
                    carList.add(carDao.queryOne(recommend.getCar().getCarId()));
                });
        return carList;
    }

    @Override
    public List getCarList(int userId) {
        List carList = new LinkedList();
        carList.add(getRecommendCars(userId));
        List<Nav> navList = navDao.queryAll();
        navList.stream()
                .forEach(nav -> {
                    if (nav.getNavId() != 1 ) {
                        carList.add(carDao.queryByType(nav.getCarType().getTypeId()));
                    }
                });
        return carList;
    }
}
